<?php
/**
	ZFWPager
	Pager for the Zend Framework
	Richard Gifford / Think.Create.Com
	richard.gifford [at] thinkcreate.com.au
	http://www.thinkcreate.com.au
**/
class ZFWPager {
	
	// The number of records per page
	protected $records_per_page = 10; 
	// The number of pages to display on the page navigator
	protected $pages_per_nav = 10;
	// The current page
	public $current_page = 1;
	// The Zend_Db_Table object which we're using
	protected $table = null;
	// The query where clause
	protected $where = null;
	// The query order clause
	protected $order = null;
	// Number of records returned by the query
	protected $query_results = array();
	
	
	public function __construct($table, $where = null, $page = 1, $order = null, $records_per_page = 10) {
		$this->table = $table;
		$this->where = $where;
		$this->current_page = $page;
		$this->records_per_page = $records_per_page;
		$this->order = $order;
	}
	
	// Return the total number of pages in the set
	public function pageCount() {
		if ( isset( $this->query_results[page_count] ) ) {
			return $this->query_results[page_count];
		} else {
			$this->query_results[page_count] = ceil( $this->recordCount() / $this->records_per_page );
			return $this->query_results[page_count];
		}
	}
	
	// Return the total number of records in the set
	public function recordCount() {
		if ( isset( $this->query_results[record_count] ) ) {
			return $this->query_results[record_count];
		} else {
			//$result = $this->table->fetchCols(new Zend_Db_Expr('count(*) as record_count'), $this->where);
			//$this->query_results[record_count] = (integer) $result->current()->record_count;
			$this->query_results[record_count] = (int) count($this->table->fetchAll($this->where));
			return $this->query_results[record_count];
		}
	}
	
	// Return the record set for the current page
	public function records() {
		if ( isset( $this->query_results[records] ) ) {
			return $this->query_results[records];
		} else {
			$this->query_results[records] = $this->table->fetchAll($this->where, $this->order, $this->records_per_page, $this->offset());
			return $this->query_results[records];
		}
	}
	
	public function firstRecord() {
		return $this->offset();
	}
	
	public function lastRecord() {
		if ( $this->firstRecord() > 1 ) {
			return $this->firstRecord() + count( $this->records() );
		} else {
			return count( $this->records() );
		}
	}
	
	private function offset() {
		if ( isset( $this->query_results[offset] ) ) {
			return $this->query_results[offset];
		} else {
			if ( $this->current_page > 1 ) {
				$this->query_results[offset] = ( $this->current_page - 1 ) * $this->records_per_page;
			} else { 
				$this->query_results[offset] = 1;
			}
		}
		return $this->query_results[offset];
	}
	
	public function nextPage() {
		if ( $this->current_page < $this->pageCount() ) {
			return $this->current_page + 1;
		}
	}
	
	public function previousPage() {
		if ( $this->current_page > 1 ) {
			return $this->current_page - 1;
		}
	}
	
	public function pageRange() {
		$start = 1;
		$end = $this->pages_per_nav;
		if ( $this->pageCount() > $this->pages_per_nav) {
			if ( $this->current_page >= ( $this->pages_per_nav / 2 ) ) { 
				$start = $this->current_page - ( ceil( $this->pages_per_nav / 2 ) - 1 );
				$end = $this->current_page + ceil( $this->pages_per_nav / 2 );
				if ( $end > $this->pageCount() ) { 
					$end = $this->pageCount();
				}
			} else { 
				$end = $this->pages_per_nav; 
			}
		} else {
			$end = $this->pageCount();
		}
		return array( 
			'start' => (int) $start, 
			'end' => (int) $end 
		);
	}
	
	public function pageRangeStart() { 
		$page_range = $this->pageRange();
		return $page_range['start'];
	}
	
	public function pageRangeEnd() {
		$page_range = $this->pageRange();
		return $page_range['end'];
	}
	
}

?>